Compress files using the Java ZIP API :: 자바네트워크I/O[SSISO Community]
 
SSISO 카페 SSISO Source SSISO 구직 SSISO 쇼핑몰 SSISO 맛집
추천검색어 : JUnit   Log4j   ajax   spring   struts   struts-config.xml   Synchronized   책정보   Ajax 마스터하기   우측부분

자바네트워크I/O
[1]
등록일:2008-03-12 09:58:10 (0%)
작성자:
제목:Compress files using the Java ZIP API
import  java.io.File;
import  java.io.FileInputStream;
import  java.io.FileOutputStream;
import  java.io.IOException;
import  java.util.zip.GZIPOutputStream;
import  java.util.zip.ZipEntry;
import  java.util.zip.ZipOutputStream;

public  class  Compress  {
    public  static  void  gzipFile(String  from,  String  to)  throws  IOException  {
        FileInputStream  in  =  new  FileInputStream(from);
        GZIPOutputStream  out  =  new  GZIPOutputStream(new  FileOutputStream(to));
        byte[]  buffer  =  new  byte[4096];
        int  bytesRead;
        while  ((bytesRead  =  in.read(buffer))  !=  -1)
            out.write(buffer,  0,  bytesRead);
        in.close();
        out.close();
    }

    /**  Zip  the  contents  of  the  directory,  and  save  it  in  the  zipfile  */
    public  static  void  zipDirectory(String  dir,  String  zipfile)
            throws  IOException,  IllegalArgumentException  {
        //  Check  that  the  directory  is  a  directory,  and  get  its  contents
        File  d  =  new  File(dir);
        if  (!d.isDirectory())
            throw  new  IllegalArgumentException("Not  a  directory:    "
                    +  dir);
        String[]  entries  =  d.list();
        byte[]  buffer  =  new  byte[4096];  //  Create  a  buffer  for  copying
        int  bytesRead;

        ZipOutputStream  out  =  new  ZipOutputStream(new  FileOutputStream(zipfile));

        for  (int  i  =  0;  i  <  entries.length;  i++)  {
            File  f  =  new  File(d,  entries[i]);
            if  (f.isDirectory())
                continue;//Ignore  directory
            FileInputStream  in  =  new  FileInputStream(f);  //  Stream  to  read  file
            ZipEntry  entry  =  new  ZipEntry(f.getPath());  //  Make  a  ZipEntry
            out.putNextEntry(entry);  //  Store  entry
            while  ((bytesRead  =  in.read(buffer))  !=  -1)
                out.write(buffer,  0,  bytesRead);
            in.close();  
        }
        out.close();
    }

    public  static  void  main(String  args[])  throws  IOException  {
        String  from  =  ".";
        File  f  =  new  File(from);
        boolean  directory  =  f.isDirectory();  //  Is  it  a  file  or  directory?

        Compress.zipDirectory(from,  from  +  ".zip");
        Compress.gzipFile(from,  from  +  ".gz");
    }
}
[본문링크] Compress files using the Java ZIP API
[1]
코멘트(이글의 트랙백 주소:/cafe/tb_receive.php?no=2521
작성자
비밀번호

 

SSISOCommunity

[이전]

Copyright byCopyright ⓒ2005, SSISO Community All Rights Reserved.